package com.handeasy;

import com.handeasy.service.CallWebService;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class AddCommentDialog extends DialogFragment {

	private static final String SOAP_ACTION_CREER_COMM = "http://session.handeasy.com/createComment";
    private static final String METHOD_NAME_CREER_COMM = "createComment";
    private static final String NAMESPACE = "http://session.handeasy.com/";
    private static final String URL_COMM = "http://192.168.1.31:7070/CommentSessionBean/CommentSessionBean?WSDL";
	
	
	public View mView;
	public EditText mEditComm;
	public CheckBox mChckBxAlert;
	public HandMarker mMarker;
	
	public Double mLat;
	public Double mLong;
	
	
	/**
	 * Constructeur
	 * 
	 * @param lLat
	 * @param lLong
	 * @return
	 */
	public static AddCommentDialog newInstance(Double lLat, Double lLong) {
		AddCommentDialog frag = new AddCommentDialog();
		frag.setmLat(lLat);
		frag.setmLong(lLong);
		//frag.setmMarker(lMarker);
		return frag;
	}

	/**
	 * @return the mMarker
	 */
	public HandMarker getmMarker() {
		return mMarker;
	}

	/**
	 * @param mMarker the mMarker to set
	 */
	public void setmMarker(HandMarker mMarker) {
		this.mMarker = mMarker;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.support.v4.app.DialogFragment#onCreateDialog(android.os.Bundle)
	 */
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Builder build = new AlertDialog.Builder(getActivity()).setTitle(R.string.add_new_comment);
		mView = this.getActivity().getLayoutInflater().inflate(R.layout.add_comment_dialog, null);
		
		mEditComm = (EditText) mView.findViewById(R.id.editComm);
		mChckBxAlert = (CheckBox) mView.findViewById(R.id.chckbxAlert);
		
		
		build.setPositiveButton(R.string.post_new_comment, new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
				if(mEditComm.getText().toString().length() == 0){
					Toast.makeText(getActivity(), "Please type your comment !", Toast.LENGTH_LONG).show();
				}
				else if(getmLat() == null || getmLong() == null){
					Toast.makeText(getActivity(), "Problem, lat/long object : " + getmLat(), Toast.LENGTH_LONG).show();
				}
				else{
					CallWebService ws = new CallWebService(SOAP_ACTION_CREER_COMM, METHOD_NAME_CREER_COMM, NAMESPACE, URL_COMM);
					ws.startServiceAddComment(getmLat(), getmLong(), mEditComm.getText().toString(), mChckBxAlert.isChecked());
					Toast.makeText(getActivity(), "Sending your comment", Toast.LENGTH_LONG).show();
					dialog.dismiss();
				}
				
				
			}
		});
		
		
		build.setNegativeButton(android.R.string.cancel, new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				
			}
		});
		
		build.setView(mView);
		return build.create();
	}

	/**
	 * @return the mLat
	 */
	public Double getmLat() {
		return mLat;
	}

	/**
	 * @param mLat the mLat to set
	 */
	public void setmLat(Double mLat) {
		this.mLat = mLat;
	}

	/**
	 * @return the mLong
	 */
	public Double getmLong() {
		return mLong;
	}

	/**
	 * @param mLong the mLong to set
	 */
	public void setmLong(Double mLong) {
		this.mLong = mLong;
	}
	
	

}
